package cn.com.pajx.pajx_visitor_v3.api;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.net.URLDecoder;

import cn.com.pajx.pajx_visitor_v3.mvp.model.IBaseModel;
import cn.com.pajx.pajx_visitor_v3.utils.AESUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;
import cn.com.pajx.pajx_visitor_v3.utils.ToastUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * author :rcw.
 * date :2018/5/22 10:03.
 */

public class HttpUtils {

    public static <T> Disposable getDisposable(Observable<HttpResponse<T>> observable, final String aesKey, final String which, final IBaseModel.IOnRequestListener onRequestListener) {
        Disposable disposable;
        disposable = observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(tHttpResponse -> {
                    if (onRequestListener != null && tHttpResponse != null) {
                        //解决url路径中特殊字符转义问题
                        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                        String data = gson.toJson(tHttpResponse.getData());
                        String message = tHttpResponse.getMessage();
                        int code = tHttpResponse.getStatus();
                        if (code == 200) {
                            String decoderData = URLDecoder.decode(data, "UTF-8").replaceAll("\"", "");
                            LogUtils.e("decoderData=" + decoderData);
                            String decrypt_data;
                            //兼容数据为空和布尔类型
                            if (decoderData.equals("null") || TextUtils.isEmpty(decoderData)) {
                                decrypt_data = "";
                            } else if (decoderData.equals("true") || decoderData.equals("false")) {
                                decrypt_data = decoderData;
                            } else {
                                decrypt_data = AESUtil.decryptCBC(decoderData, AppConfig.AES_KEY);
                            }
                            LogUtils.e("decrypt_data=" + decrypt_data);
                            onRequestListener.onRequestSuccess(decrypt_data, aesKey, message, code, which);
                        } else {
                            onRequestListener.onRequestError(message, code, which);
                        }

                    } else {
                        ToastUtil.toast("数据获取失败");
                    }
                }, throwable -> {
                    if (onRequestListener != null) {
                        ExceptionHandle.handleException(throwable);
                        throwable.printStackTrace();
                        onRequestListener.onRequestFail(throwable, which);
                    }
                });
        return disposable;
    }
}
